home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / application.py next >
Encoding:
Python Source  |  2009-04-27  |  4.8 KB  |  154 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import sys
  20. import logging
  21. import posixpath
  22.  
  23. from gettext import gettext as _
  24.  
  25. from logging import StreamHandler, FileHandler, Formatter
  26. from optparse import OptionParser
  27.  
  28. from checkbox.contrib import bpickle_registry
  29.  
  30. from checkbox.lib.config import Config
  31. from checkbox.lib.environ import get_variable
  32.  
  33. from checkbox.plugin import PluginManager
  34. from checkbox.reactor import Reactor
  35. from checkbox.registry import RegistryManager
  36.  
  37.  
  38. def parse_string(options):
  39.     args = []
  40.     while True:
  41.         options = options.strip()
  42.         if not options:
  43.             break
  44.  
  45.         index = 0
  46.         while index < len(options) \
  47.               and (not options[index].isspace() \
  48.                   or options[index - 1] == "\\"):
  49.            index += 1
  50.  
  51.         args.append(options[:index])
  52.         options = options[index:]
  53.  
  54.     return args
  55.  
  56.  
  57. class Application(object):
  58.  
  59.     reactor_factory = Reactor
  60.  
  61.     def __init__(self, config):
  62.         self._config = config
  63.         self.reactor = self.reactor_factory()
  64.  
  65.         # Registry manager setup
  66.         self.registry = RegistryManager(self._config)
  67.  
  68.         # Plugin manager setup
  69.         self.plugin_manager = PluginManager(self._config,
  70.             self.reactor, self.registry)
  71.  
  72.     def run(self):
  73.         try:
  74.             bpickle_registry.install()
  75.             self.reactor.run()
  76.             bpickle_registry.uninstall()
  77.         except:
  78.             logging.exception("Error running reactor.")
  79.             raise
  80.  
  81.  
  82. class ApplicationManager(object):
  83.  
  84.     application_factory = Application
  85.  
  86.     default_log_level = "critical"
  87.  
  88.     def parse_options(self, args):
  89.         usage = _("Usage: checkbox [OPTIONS]")
  90.         parser = OptionParser(usage=usage)
  91.         parser.add_option("--version",
  92.                           action="store_true",
  93.                           help=_("Print version information and exit."))
  94.         parser.add_option("-l", "--log",
  95.                           metavar="FILE",
  96.                           help=_("The file to write the log to."))
  97.         parser.add_option("--log-level",
  98.                           default=self.default_log_level,
  99.                           help=_("One of debug, info, warning, error or critical."))
  100.         parser.add_option("-c", "--config",
  101.                           action="append",
  102.                           type="string",
  103.                           default=[],
  104.                           help=_("Configuration override parameters."))
  105.         return parser.parse_args(args)
  106.  
  107.     def create_application(self, args=sys.argv):
  108.         # Prepend environment options
  109.         string_options = get_variable("CHECKBOX_OPTIONS", "")
  110.         args[:0] = parse_string(string_options)
  111.         (options, args) = self.parse_options(args)
  112.  
  113.         log_level = logging.getLevelName(options.log_level.upper())
  114.         log_handlers = []
  115.         if options.log:
  116.             log_filename = options.log
  117.             log_handlers.append(FileHandler(log_filename))
  118.         else:
  119.             log_handlers.append(StreamHandler())
  120.  
  121.         # Logging setup
  122.         format = ("%(asctime)s %(levelname)-8s %(message)s")
  123.         if log_handlers:
  124.             for handler in log_handlers:
  125.                 handler.setFormatter(Formatter(format))
  126.                 logging.getLogger().addHandler(handler)
  127.             if log_level:
  128.                 logging.getLogger().setLevel(log_level)
  129.         elif not logging.getLogger().handlers:
  130.             logging.disable(logging.CRITICAL)
  131.  
  132.         # Config setup
  133.         if len(args) != 2:
  134.             sys.stderr.write(_("Missing configuration file as argument.\n"))
  135.             sys.exit(1)
  136.  
  137.         config_file = posixpath.expanduser(args[1])
  138.         config = Config(config_file, options.config)
  139.  
  140.         section_name = "checkbox/plugins/client_info"
  141.         section = config.get_section(section_name)
  142.         if not section:
  143.             section = config.add_section(section_name)
  144.         section.set("name", posixpath.basename(config.path) \
  145.             .replace(".ini", ""))
  146.         section.set("version", config.get_defaults().version)
  147.  
  148.         # Check options
  149.         if options.version:
  150.             print config.get_defaults().version
  151.             sys.exit(0)
  152.  
  153.         return self.application_factory(config)
  154.